package com.atguigu.tingshu.order.strategy.impl;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

import com.atguigu.tingshu.album.client.AlbumInfoFeignClient;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.order.hander.OrderTradeNoHandler;
import com.atguigu.tingshu.user.client.UserInfoFeignClient;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.google.common.collect.Lists;

import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.order.strategy.OrderTradeStrategy;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * TrackOrderTradeStrategy 声音购买策略类
 *
 * @author Petrichor
 * @version 2024/01/05 20:46
 **/
@Service
@Slf4j
public class TrackOrderTradeStrategy extends OrderTradeNoHandler implements OrderTradeStrategy {
    private final RedisTemplate<String,String> redisTemplate;
    private final UserInfoFeignClient userInfoFeignClient;
    private final AlbumInfoFeignClient albumInfoFeignClient;
    public TrackOrderTradeStrategy(RedisTemplate<String, String> redisTemplate, UserInfoFeignClient userInfoFeignClient, AlbumInfoFeignClient albumInfoFeignClient) {
        this.redisTemplate = redisTemplate;
        this.userInfoFeignClient = userInfoFeignClient;
        this.albumInfoFeignClient = albumInfoFeignClient;
    }

    /**
     * @description: 定义获取订单确认页面所需要的接口方法
     * @author Petrichor
     * @date 2024/1/5 20:43
     * @param tradeVo 订单确认对象
     * @return OrderInfoVo 订单对象
     **/
    @Override
    public OrderInfoVo trade(TradeVo tradeVo) {
        // 付款项目类型Id 即声音id
        Long itemId = tradeVo.getItemId();
        // 创建返回对象
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        // 根据购买集数 判断 是否正确
        Integer trackCount = tradeVo.getTrackCount();
        if(trackCount < 0) {
            throw new GuiguException(ResultCodeEnum.ARGUMENT_VALID_ERROR);
        }

        // <3>付款项目类型: 1001-专辑 1002-声音 1003-vip会员
        orderInfoVo.setItemType(tradeVo.getItemType());
        // 根据专辑id 和 购买集数 获取下单声音列表
        Result<List<TrackInfo>> paidTrackInfoList = albumInfoFeignClient.findPaidTrackInfoList(itemId, trackCount);
        List<TrackInfo> trackInfoList = paidTrackInfoList.getData();

        // 获取专辑id， 根据专辑的id查询专辑数据
        Long albumId = trackInfoList.get(0).getAlbumId();
        Result<AlbumInfo> albumInfoResult = albumInfoFeignClient.getAlbumInfo(albumId);
        AlbumInfo albumInfo = albumInfoResult.getData();

        // <4>订单原始金额
        BigDecimal originalAmount =  trackCount > 0 ? albumInfo.getPrice().multiply(new BigDecimal(trackCount)) : albumInfo.getPrice() ;
        orderInfoVo.setOriginalAmount(originalAmount);
        // <6>订单总金额
        orderInfoVo.setOrderAmount(originalAmount);


        // <7>订单明细列表
        // 明细数据
        List<OrderDetailVo> orderDetailVoList = trackInfoList.stream().map(trackInfo -> {
            OrderDetailVo orderDetailVo = new OrderDetailVo();
            orderDetailVo.setItemId(trackInfo.getId());
            orderDetailVo.setItemName(trackInfo.getTrackTitle());
            orderDetailVo.setItemUrl(trackInfo.getCoverUrl());
            orderDetailVo.setItemPrice(albumInfo.getPrice());
            return orderDetailVo;
        }).collect(Collectors.toList());
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);

        // 单独购买集数 没有减免
        // <5>减免总金额
        orderInfoVo.setDerateAmount(new BigDecimal("0.00"));
        // <8>订单减免明细列表
        orderInfoVo.setOrderDerateVoList(Lists.newArrayList());

        // <1>交易号
        // <2>支付方式：1101-微信 1102-支付宝 1103-账户余额
        // <9>时间戳
        // <10>签名
        this.orderTradeNoHandle(orderInfoVo , redisTemplate);
        return orderInfoVo;
    }
    /**
     * @description: 定义获取对应的付款项目
     * @author Petrichor
     * @date 2024/1/5 20:44
     * @return 获取对应的付款项目
     **/
    @Override
    public String getItemType() {
        return SystemConstant.ORDER_ITEM_TYPE_TRACK;
    }
}


